package com.njtcedu.microblog.website.home.web;

import java.io.IOException;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.njtcedu.microblog.intranet.systemconfig.service.SystemConfigService;
import com.njtcedu.microblog.intranet.systemconfig.web.response.AdImageResponse;
import com.njtcedu.microblog.website.content.service.ContentService;
import com.njtcedu.microblog.website.home.service.HomeService;
import com.njtcedu.microblog.website.home.web.request.UserRegisterRequest;
import com.njtcedu.microblog.website.home.web.response.CurrentUser;

@Controller
public class HomeController {
	
	@Inject
	private HomeService homeService;
	
	@Inject
	private ContentService contentService;
	
	@Inject
	private SystemConfigService systemConfigService;
	
	@RequestMapping(value="/home", method = RequestMethod.GET )
	public ModelAndView home(Map<String, Object> map) {
		//map.put("contentsResponse", contentService.getAllContents());
		ModelAndView model = new ModelAndView("website/home");
		model.addObject("contentsResponse", contentService.getAllContents());
		model.addObject("images", systemConfigService.getAdImages());
		model.addObject("topusers", homeService.getTop10User());
		return model;
	}

	@RequestMapping(value="/register", method = RequestMethod.GET )
	public ModelAndView toRegisterPage() {
		return new ModelAndView("website/register");
	}
	
	@RequestMapping(value="/login", method = RequestMethod.GET )
	@ResponseBody
	public String userLogin(HttpServletRequest httpRequest, @RequestParam(value = "account", required = false) String account, @RequestParam(value = "password", required = false) String password) {
		
		try {
			CurrentUser user = homeService.login(account, password);
			httpRequest.getSession().setAttribute("currentUser", user);
		} catch (Exception e) {
			return e.getMessage();
		}
		
		return "true";
	} 
	
	@RequestMapping(value="/register", method = RequestMethod.POST )
	public ModelAndView register(UserRegisterRequest request) {
		homeService.register(request);
		return new ModelAndView("website/home");
	} 
	
	@RequestMapping(value="/userinfo", method = RequestMethod.GET )
	public String userInfo() {
		return "user_info";
	} 
	
	@RequestMapping(value="/userinfo/passwd", method = RequestMethod.GET )
	public String userInfoPasswd() {
		return "user_passwd";
	} 
	@RequestMapping(value="/userinfo/image", method = RequestMethod.GET )
	public String userInfoImage() {
		return "user_image";
	} 
	
	
	@RequestMapping(value = "/user/image", method = RequestMethod.POST )
	@ResponseBody
	public AdImageResponse uploadAdImage(@RequestParam(value = "imageFile", required = false) MultipartFile imageFile, HttpServletRequest request) throws IOException {
		String evePath = request.getServletContext().getRealPath("/");
		CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute("currentUser"); 
		AdImageResponse response = homeService.uploadAdImage(imageFile, evePath, currentUser);
		request.getSession().removeAttribute("currentUser");
		currentUser.setUserImage(response.getImageUrl());
		request.getSession().setAttribute("currentUser", currentUser);
		return response;
	}
	
}
